const BehaviorAll = require("../../../behaviors/behavior_all.js");
const Pay_wx = require("../../../utils/pay_wx.js");
const ServiceOrder = require("../../../service/service_order.js");
const ServicePay = require("../../../service/service_pay.js");
const BehaviorAddress = require("../../../template/address/behavior.js");

module.exports = {
  /**
   * behaviors
   */
  behaviors: [BehaviorAll, BehaviorAddress],
  /**
   * 页面的初始数据
   */
  data: {
    /* 地址配置 */
    b_address_addressExistDefaultHideAddShowFlag: true,
    b_address_addressInfoShowFlag: true,
    b_address_addressInfoModifyShowFlag: true,
    b_address_addressInfoListShowFlag: true,
    //
    b_address_addressServiceCreateFlag: false,
    b_address_addressServiceModifyFlag: false,
    b_address_addressServiceDeleteFlag: false,
    //
    b_address_addressListSelectShowFlag: true,
    b_address_addressListEditShowFlag: false,
    b_address_addressListDeleteShowFlag: false,
    /**
     * 支付方式：
     *  微信支付 == 1
     *  线下支付 == 2
     */
    payWay: 1,
    /**
     * 订单相关
     */
    order: null,
    orderComment: null
  },
  /**
   * 方法
   */
  methods: {
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      const _this = this;
      const orderId = options.id;
      ServiceOrder.findByIdWithProducts(orderId, function (data) {
        let order = data;
        _this.setData({
          order,
          b_address_addressData: { 
            id: null, 
            username: order.username, 
            phone: order.phone, 
            detailAddress: order.address, 
            area: order.fixAddress, 
            townId: order.townId, 
            defaultFlag: false
          }
        })
      });
    },
    /**
     * 切换支付
     */
    onPayWayChange: function (e) {
      const _this = this;
      _this.setData({
        payWay: e.detail.value
      });
    },
    /**
     * 修改订单备注
     */
    onChangeOrderComment: function (e) {
      const _this = this;
      const comment = e.detail.value;
      _this.setData({ orderComment: comment });
    },
    /**
     * 支付
     */
    onPay: function (e) {
      const _this = this;
      const address = _this.data.b_address_addressData;
      // 验证地址
      if (!_this.b_address_validateAddressData(e)) {
        return;
      }
      // 验证支付方式
      const payWay = Number(_this.data.payWay);
      if (payWay !== 1 && payWay !==2){
        wx.showToast({
          title: '请选择支付方式',
          icon: 'none'
        })
        return;
      }
      // 支付
      const ticketId = null;
      const currentScore = 0;
      const orderId = _this.data.order.id;
      const sendData = { address, comment: _this.data.orderComment, score: currentScore};
      if (payWay === 1) {
        _this._payWithWx(orderId, ticketId, sendData);
      } else if (payWay === 2) {
        _this._payWithCard(orderId, ticketId, sendData);
      }
    },
    /**
     * 微信支付
     */
    _payWithWx: function (orderId, ticketId, data) {
      // 发起预支付请求
      ServicePay.preparePay(orderId, ticketId, data, function (data, res) {
        if (data === 'Finish') {
          wx.navigateTo({ url: '../../../model_user/pages/order/order?state=3' })
          return;
        }
        Pay_wx.wxRequestPayment(
          data,
          function (res) {
            wx.navigateTo({ url: '../../../model_user/pages/order/order?state=3' })
          }
        );
      })
    },

    /**
     * 线下支付
     */
    _payWithCard: function (orderId, ticketId, data) {
      const _this = this;
      // const totalPrice = _this.data.order.totalPrice;
      // const ticket = _this.data.ticket;
      // const ticketPrice = ticket ? ticket.price : 0;
      ServicePay.payUnderLine(
        orderId,
        ticketId,
        data,
        function () {
          wx.navigateTo({ url: '../../../model_user/pages/order/order?state=3' })
        }
      );
      // }
    }

  }

};